package TheGame.Entity.Mob;

import TheGame.Graphics.Screen;
import TheGame.Graphics.Sprite;
import TheGame.Input.FireThread;
import TheGame.Input.Keyboard;
import TheGame.Levels.Level;

public class Player extends Mob{
	
	private Keyboard input;
	private int anim = 0;
	private boolean walking = false;
	private FireThread fire;
	private boolean firing = false;
	
	
	public Player(Keyboard in){
		input = in;
	}
	
	public Player(int x, int y, Keyboard in){
		this.x = x;
		this.y = y;
		input = in;
	}
	
	public int getHealth(){
		return health;
	}
	
	public void update(Level level){
		level.update();
		int xa = 0, ya = 0;
		if (anim < 6000) anim++; else anim = 0;
		if (input.up) ya--;
		if (input.down) ya++;
		if (input.left) xa--;
		if (input.right) xa++;
		
		
		
		if ((xa != 0 || ya !=0) && !input.fire){
			if (xa != 0 && ya != 0){
				if (!collision(level, xa, 0)) move(xa, 0);
				if (!collision(level, 0, ya)) move(0, ya);
			}
			else if (!collision(level, xa, ya)) move(xa, ya);
			walking = true;
		}
		else{
			walking = false;
		}
		
		int[] x1 = level.getBadX();
		int[] y1 = level.getBadY();
		
		for (int i = 0; i < x1.length; i++){
			for (int j = 0; j < y1.length; j++){
				if (x1[i] == x >> 4 && y1[j] == y >> 4){
					level.displayPanel();
					if (input.interact){
						level.changePath((y >> 4) / 4 - 1);
						x = 16 * 8;
						y = 16 * 4;
					}
						
				}
			}
		}
	}
	
	public void render(Screen screen){
		if (input.fire && !firing) fire();
		else if (!firing){
		if (dir == 0){
			sprite = Sprite.player_up;
			if (walking){
				if (anim % 20 < 10){
					sprite = Sprite.player_up_1;
				}
				else{
					sprite =Sprite.player_up_2;
				}
			}
		}
		if (dir == 2){
			sprite = Sprite.player_down;
			if (walking){
				if (anim % 20 < 10){
					sprite = Sprite.player_down_1;
				}
				else{
					sprite =Sprite.player_down_2;
				}
			}
		}
		if (dir == 3){
			sprite = Sprite.player_left;
			if (walking){
				if (anim % 30 < 10){
					sprite = Sprite.player_left_1;
				}
				else if (anim % 30 < 20){
					sprite = Sprite.player_left;
				}
				else{
					sprite =Sprite.player_left_2;
				}
			}
		}
		if (dir == 1){
			sprite = Sprite.player_right;
			if (walking){
				if (anim % 30 < 10){
					sprite = Sprite.player_right_1;
				}
				else if (anim % 30 < 20){
					sprite = Sprite.player_right;
				}
				else{
					sprite =Sprite.player_right_2;
				}
			}
		}
		}
		
		
		screen.renderMob(x - 16, y - 16, sprite);

		/*
		screen.renderPlayer(xx, yy, Sprite.player0);
		screen.renderPlayer(xx + 16, yy, Sprite.player1);
		screen.renderPlayer(xx, yy + 16, Sprite.player2);
		screen.renderPlayer(xx + 16, yy + 16, Sprite.player3);
		*/

		
	}
	
	
	public boolean collision(Level level, int xa, int ya){
		//I am calling this system of hit detection....point intersection
		int[] XPoints = {x, x + 10, x - 10};
		int[] YPoints = {y, y + 15, y - 10};
		//these arrays create a total of 9 points around the player if any of these points hits a solid tile the method returns true!!!!
		for (int i = 0; i < XPoints.length; i++){
			for (int j = 0; j < YPoints.length; j++){
				if (level.getTile((XPoints[i] + xa) >> 4,(YPoints[j] + ya) >> 4).isSolid())
					return true;
				Mob[] mobs = level.getMobs();
				if (mobs != null){
					for (int c = 0; c < mobs.length; c++){
						System.out.println(x + " " + y);
						System.out.println(mobs[c].getX() + " " + mobs[c].getY());
						if ((mobs[c].getX() >> 4 == (XPoints[i] + xa) >> 4 || mobs[c].getX() >> 4 == ((XPoints[i] + xa) >> 4) - 1) && mobs[c].getY() >> 4 == (YPoints[j] + ya) >> 4)
							return true;
					}
				}
			}
		}
		return false;
	}
	
	public void fire(){
		/*if (dir == 1){
			if (anim % 30 < 10) sprite = Sprite.player_right_shotgun_1;
			else if (anim % 30 < 20 ) sprite = Sprite.player_right_shotgun_2;
			else sprite = Sprite.player_right_shotgun_3;
		}
		else if (dir == 3){
			if (anim % 30 < 10) sprite = Sprite.player_left_shotgun_1;
			else if (anim % 30 < 20 ) sprite = Sprite.player_left_shotgun_2;
			else sprite = Sprite.player_left_shotgun_3;
		}
		*/
		firing = true;
		fire = new FireThread(this);
		fire.start();
	}
	
	public void setFire(){
		firing = false;
	}
	
	
}
